﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AlexandriaPressAuthorObject;
using System.Collections.Specialized;
using System.IO;
using AlexandriaPressAuthorBusinessLayer;

namespace AlexandriaPressWebsite
{
    public partial class PublishToSite : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            BusinessAuthor bus = new BusinessAuthor();
            if (!Page.IsPostBack)
            {
                if (HttpContext.Current.User.Identity.IsAuthenticated)
                {
                    if (bus.validateUserIfAuthor(bus.getAuthorID(HttpContext.Current.User.Identity.Name)))
                    {
                        lblStocks.Visible = false;
                        lblPrice.Visible = false;
                        lblDescription.Visible = false;

                        BusinessAuthor business = new BusinessAuthor();
                        if (Request.QueryString["id"] == null)
                        {
                            txtDraftTitle.Visible = false;
                            txtDraftGenre.Visible = false;
                            txtDraftDate.Visible = false;
                            txtDraftComment.Visible = false;
                            txtDraftStat.Visible = false;
                            Label6.Visible = false;
                            Label7.Visible = false;
                            Label8.Visible = false;
                            Label9.Visible = false;
                            Label10.Visible = false;
                            Label12.Visible = false;
                            Label13.Visible = false;
                            Label14.Visible = false;
                            Button1.Visible = false;
                            Label16.Visible = true;
                            Label16.Text = "INVALID ATTEMPT TO ACCESS PAGE";

                        }
                        else
                        {
                            Draft draft = business.viewDraft(Convert.ToInt32(Request.QueryString["id"]), business.getAuthorID(HttpContext.Current.User.Identity.Name));

                            txtDraftTitle.Text = draft.WorkingTitle;
                            txtDraftGenre.Text = draft.Genre;
                            txtDraftDate.Text = draft.DateSubmitted.ToString();
                            txtDraftComment.Text = draft.Comments;
                            txtDraftStat.Text = draft.ApprovalStatus;
                            Image2.ImageUrl = draft.BookCoverLocation;
                            Session["DraftID"] = draft.DraftID;
                            Button1.Visible = true;
                        }
                    }
                    else
                    {
                        Response.Redirect("~/MasterPages/UserPages/LoggedInUserPage.aspx");
                    }

                }
                else
                {
                    Response.Redirect("~/MasterPages/MainPage.aspx");
                }
            }

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            BusinessAuthor bus = new BusinessAuthor();
            Draft draft = new Draft();

            draft = bus.viewDraft((int)Session["DraftID"], bus.getAuthorID(HttpContext.Current.User.Identity.Name));

            Label16.Text =  bus.publishDraftToSite(draft.DraftID, draft.WorkingTitle, draft.Genre, draft.AuthorID, TextArea1.Value, Convert.ToInt32(txtDraftPrice.Text), Convert.ToInt32(txtBookStocks.Text));

            Book book = new Book();
            book = bus.viewMyBook(bus.getBookID(draft.WorkingTitle),draft.AuthorID);

            if (bus.publishValidator(Label16.Text))
            {
                txtDraftPrice.Visible = false;
                TextArea1.Visible = false;
                txtBookStocks.Visible = false;

                lblStocks.Visible = true;
                lblPrice.Visible = true;
                lblDescription.Visible = true;
                lblDescription.Text = book.Description;
                lblPrice.Text = book.Price.ToString();
                lblStocks.Text = book.Stock.ToString();

            }
        }
    }
}
